<template>
  <div class="serach-result">
    <!-- 导航栏 -->
    <van-nav-bar
    :title= "keyword + '的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
     >
      <van-cell
        v-for="item in list"
        :key="item.art_id.toString()"
        :title="item.title"
        @click="$router.push('/article/' + item.art_id.toString() )"
      />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
// 引入接口
import { getSearch } from '@/api/search'
export default {
  name: 'SearchResult',
  data () {
    return {
      keyword: '',
      list: [],
      loading: false,
      finished: false,
      curPage: 1 // 当前是第几页
    }
  },
  created () {
  // 获取传过来的关键字
    var keyword = this.$route.query.keyword
    this.keyword = keyword
    // alert(keyword)
    this.onLoad()
  },
  methods: {
    async onLoad () {
      // 发送请求获取数据
      const result = await getSearch(this.$route.query.keyword, this.curPage)
      // 把请求回来的数据添加到 list中
      const arr = result.data.data.results
      this.list.push(...arr)
      // s手动结束loading状态
      this.loading = false
      // 判断当前是否全部加载完毕
      if (arr.length === 0) {
        this.finished = true
      }
      // 跟新页码
      this.curPage++
    }
  }
}
</script>

<style lang="less" scoped>
.serach-result {
  height: 100%;
  overflow: auto;
  .article-list {
    margin-top: 39px;
  }
}
</style>
